<?

$_table = 'CR_CLASSIFICATOR_VALUE o';
$_parent_key = 'classificatorvalue_id';

$_magic = "classificator_id=1 AND state='A' AND onsite=1";

$res = array();
//dbdebug();

if (($id = gccHuuid(param('id'))) || (($id = param('id')) == 'null')) {
	$_crit = "$_magic AND ".(($id == 'null') ? "$_parent_key IS NULL" : "$_parent_key=$id");

	if (sizeof($d = datafetch_array('db_list',
		array('table' => $_table, 'index' => 'id', 
			'where' => $_crit,
			'order' => 'name',
			'select' => 'id, name'
			)))) {
			
		$keys = implode(',', array_keys($d));
		$chi = datafetch_array('db_list', array(
			'table' => $_table, 
			'index' => 'parent_id', 
			'where' => "$_parent_key IN ($keys)",
			'select' => "$_parent_key parent_id, count($_parent_key) c",
			'group' => $_parent_key,
			));


		foreach($d as $v) {
			$tmp = array('data' => $v['name'], 'attr' => array('id' => $v['id'], rel => 'folder'));
			if ( $chi[$v['id']]['c'] )
				$tmp['state'] = 'closed';

			$res[] = $tmp;
		}
	}

	header("Content-Type: application/json; charset=utf-8");

}

print json_encode($res);
exit;